package zjump.simplelibrary.utils;

import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SharedPrefUtils {

	private static final String SAVE_TAG = "SharedPrefUtils";

	private static SharedPreferences getSharedPreferences(Context context) {
		return context.getSharedPreferences(SAVE_TAG, Context.MODE_PRIVATE);
	}

	private static Editor getSharedPreferencesEditor(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(SAVE_TAG, Context.MODE_PRIVATE);
		return sharedPreferences.edit();
	}

	public static <T> void put(Context context, String key, T data) {

		Editor editor = getSharedPreferencesEditor(context);

		if (data.getClass() == Integer.class) {

			editor.putInt(key, (Integer) data);

		} else if (data.getClass() == Float.class) {

			editor.putFloat(key, (Float) data);

		} else if (data.getClass() == Long.class) {

			editor.putLong(key, (Long) data);

		} else if (data.getClass() == String.class) {

			editor.putString(key, (String) data);

		} else if (data.getClass() == Boolean.class) {

			editor.putBoolean(key, (Boolean) data);

		}
		editor.commit();
	}

	@SuppressWarnings("unchecked")
	public static <T> T get(Context context, String key) {
		SharedPreferences sharedPreferences = getSharedPreferences(context);
		Map<String, ?> map = sharedPreferences.getAll();
		return (T) map.get(key);
	}
	
	

}
